/**
 * 
 */
package gui.view.source;

import gui.controller.SourceLeftListener;
import gui.controller.SourceMainListener;
import gui.controller.SourceRightListener;
import gui.view.CustomizedColor;
import gui.view.EngineMain;
import gui.view.InputPanel;

import java.awt.BorderLayout;

import javax.swing.GroupLayout;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;

/**
 * @author Elisa Costante
 * 
 */
public class SourcesPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3754413720452076662L;

	private SourceRightPanel rightPanel;
	private SourceLeftPanel leftPanel;
	private EngineMain frame;
	private SourceRightListener rightListener;
	private SourceLeftListener leftListener;
	private SourceMainListener mainListener;

	private JInternalFrame internalFrame;

	private JDesktopPane desktopPane;

	private InputPanel inputPanel;

	public SourcesPanel(EngineMain frame) {
		super();
		this.frame = frame;
		initPanel();
		createJinternalPane();

	}

	/**
	 * 
	 */
	public void createJinternalPane() {
		desktopPane = new JDesktopPane();
		desktopPane.setBackground(CustomizedColor.BACK_GROUND_COLOR);

		internalFrame = new JInternalFrame("Input", true, false, true);
		internalFrame.setBounds(20, 20, EngineMain.MAIN_WIDTH - 50,
				EngineMain.MAIN_HEIGHT - 100);
		internalFrame.setBackground(CustomizedColor.BACK_GROUND_COLOR);

		this.add(desktopPane);

	}

	public void closeInternalFrame() {
		desktopPane.setVisible(false);
		internalFrame.setVisible(false);
		internalFrame.setBounds(20, 20, EngineMain.MAIN_WIDTH - 50,
				EngineMain.MAIN_HEIGHT - 100);

		this.setMainPanelLayout();

		leftPanel.setVisible(true);
		rightPanel.setVisible(true);

	}

	public void openInternalFrame(InputPanel inputPanel) {
		this.inputPanel = inputPanel;
		leftPanel.setVisible(false);
		rightPanel.setVisible(false);

		// Remove old components
		internalFrame.getContentPane().removeAll();
		desktopPane.removeAll();

		this.setLayout(new BorderLayout());
		desktopPane.add(internalFrame, BorderLayout.CENTER);
		internalFrame.getContentPane().add(inputPanel, BorderLayout.CENTER);
		this.add(desktopPane);

		desktopPane.setVisible(true);
		internalFrame.setVisible(true);

	}

	private void initPanel() {
		// Set the color
		this.setBackground(CustomizedColor.BACK_GROUND_COLOR);

		leftPanel = new SourceLeftPanel();
		rightPanel = new SourceRightPanel();
		// Set the listenner
		leftListener = new SourceLeftListener(leftPanel, frame);
		rightListener = new SourceRightListener(rightPanel, frame);
		mainListener = new SourceMainListener(this, rightListener);

		// Set the view with the associated Listener
		setRightPanel();
		setLeftPanel();
		setMainPanelLayout();

		// at the beginning only the left Panel is enabled
		EngineMain.setEnabledtPanel(rightPanel, false);

	}

	private void setMainPanelLayout() {
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		SequentialGroup horizontal = layout.createSequentialGroup();

		horizontal.addComponent(leftPanel, 100, 100, Short.MAX_VALUE);
		horizontal.addComponent(rightPanel, 100, 100, Short.MAX_VALUE);

		ParallelGroup vertical = layout.createParallelGroup();

		vertical.addComponent(leftPanel, 100, 100, Short.MAX_VALUE);
		vertical.addComponent(rightPanel, 100, 100, Short.MAX_VALUE);

		layout.setHorizontalGroup(horizontal);
		layout.setVerticalGroup(vertical);

	}

	private void setLeftPanel() {
		leftPanel.init(leftListener, mainListener);

	}

	private void setRightPanel() {
		rightPanel.init(rightListener, mainListener);
	}

	/**
	 * @return the rightPanel
	 */
	public SourceRightPanel getRightPanel() {
		return this.rightPanel;
	}

	/**
	 * @return the leftPanel
	 */
	public SourceLeftPanel getLeftPanel() {
		return this.leftPanel;
	}

	/**
	 * @return the frame
	 */
	public EngineMain getFrame() {
		return this.frame;
	}

	/**
	 * @return the mainListener
	 */
	public SourceMainListener getMainListener() {
		return this.mainListener;
	}

	/**
	 * @return the inputPanel
	 */
	public InputPanel getInputPanel() {
		return this.inputPanel;
	}

}
